package cn.ffcs.bpmpvc.services.impl;

import java.io.File;
import java.util.Date;
import java.util.List;

import cn.ffcs.bpm.model.ProcTranRecord;
import cn.ffcs.bpmpvc.dao.AppSmsDao;
import cn.ffcs.bpmpvc.model.AppSms;
import cn.ffcs.bpmpvc.services.AppBusinessReqService;
import cn.ffcs.bpmpvc.services.AppSmsService;
import cn.ffcs.bpmpvc.svc.ProcessPvcService;
import cn.ffcs.bpmpvc.svc.TaskAssignBean;
import cn.ffcs.bpmpvc.svc.TaskService;
import cn.ffcs.bpmpvc.web.views.ProcessInstView;
import cn.ffcs.organ.model.Person;

public class AppSmsServiceImpl implements AppSmsService {
	private ProcessPvcService processPvcService;
	private TaskService taskService;
	private AppSmsDao appSmsDao;
	private AppBusinessReqService appBusinessReqService;
	public ProcTranRecord saveAndStartFlow(String processConfigId, Person person, List<TaskAssignBean> nextTasksList, boolean isCommit, AppSms appSms, List<File> uploads,
			List<String> fileNames) throws Exception {
		// 启动流程
		ProcessInstView processInstView = getProcessPvcService().startProcessInst(processConfigId, person, nextTasksList, isCommit);
		
		
		//保存业务数据
		appSms.setReqCode(appBusinessReqService.getReqCode());
		appSms.setOpTime(new Date());
		appSms.setProcinstId(processInstView.getProcessStatus().getProcinstId().toString());
		appSmsDao.save(appSms);
		
		getAppBusinessReqService().saveDocumentType(appSms, processInstView.getProcTranRecord(), uploads, fileNames, null, processInstView.getProcTranRecord().getProcessStep());
		return processInstView.getProcTranRecord();
	}
	
	
	
	public void commitApplyTask(String flowTaskId,String content, List<TaskAssignBean> nextTasksList, boolean isCommit,AppSms appSms,List<File> uploads,
			List<String> fileNames, List<String> delDocumentIds) throws Exception {
		// 保存
		//取时间
		getAppSmsDao().save(appSms);
		ProcTranRecord procTranRecord = null;
		if (isCommit) {
			procTranRecord = getTaskService().completeTask(flowTaskId, content, nextTasksList);
		} else {
			procTranRecord = getTaskService().saveTaskAssignUser(flowTaskId, content, nextTasksList);
		}
		//
		
		getAppBusinessReqService().saveDocumentType(appSms, procTranRecord, uploads, fileNames, delDocumentIds, procTranRecord.getProcessStep());
	}
	
	public void commitTask(String flowTaskId,String content, List<TaskAssignBean> nextTasksList, boolean isCommit,List<File> uploads,List<String> fileNames, List<String> delDocumentIds) throws Exception {
		// 保存
		ProcTranRecord procTranRecord = null;
		if (isCommit) {
			procTranRecord = getTaskService().completeTask(flowTaskId, content, nextTasksList);
		} else {
			procTranRecord = getTaskService().saveTaskAssignUser(flowTaskId, content, nextTasksList);
		}
		//
		AppSms appSms=getAppSmsDao().getByProcinstId(procTranRecord.getProcinstId().toString());
		getAppBusinessReqService().saveDocumentType(appSms, procTranRecord, uploads, fileNames, delDocumentIds, procTranRecord.getProcessStep());
	}
	
	public ProcessPvcService getProcessPvcService() {
		return processPvcService;
	}
	public void setProcessPvcService(ProcessPvcService processPvcService) {
		this.processPvcService = processPvcService;
	}
	public TaskService getTaskService() {
		return taskService;
	}
	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}
	public AppSmsDao getAppSmsDao() {
		return appSmsDao;
	}
	public void setAppSmsDao(AppSmsDao appSmsDao) {
		this.appSmsDao = appSmsDao;
	}

	public AppBusinessReqService getAppBusinessReqService() {
		return appBusinessReqService;
	}

	public void setAppBusinessReqService(AppBusinessReqService appBusinessReqService) {
		this.appBusinessReqService = appBusinessReqService;
	}
	
	
}
